use strict;
use LWP::Simple;
use HTML::TreeBuilder;
use Encode;

binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');

#download the web page and store it to a file.
my $url = "http://portal.acm.org/citation.cfm?id=857077";


#my $file = "paper" . ($url =~ /id=(.*)/,$1).".html";
#print "file : " , $file , "\n";
#print getstore( $url,$file );
#$tree->parse_file( $file );


my $content = get($url);
my $tree = HTML::TreeBuilder->new();

$tree->parse( $content );

#print the title of the paper.
my $title = $tree->look_down('_tag', 'title');  
my $title_text = "NULL";
if( $title ) {
      $title_text = $title->as_text;   
} 
print "title : ",$title_text,"\n\n";

#print the publication year.

#print the Abstract.
my @abstract = $tree->look_down('class', 'abstract');  
if( @abstract ) {
	my $abstract_text = $abstract[2]->as_text;
	print "Abstract : ",$abstract_text,"\n\n";
}else {
	print "no abstract";
}

#download the paper
my $download_paper = $tree->look_down('title','Pdf');
if( $download_paper ){
	my $download_paper_url = "http://portal.acm.org/" . ($download_paper->attr('href'));
	print "paper url : ",$download_paper_url,"\n\n";
	my $file = $title_text . ".pdf";
	print "filename : ",$file,"\n";
	download_paper( $download_paper_url,$file );
}
#print the reference.
if( @abstract ){
	my @reference = $abstract[3]->look_down('class','abstract');
	if( @reference ){
		#the first is just a noise.
		shift @reference;
		foreach my $ref ( @reference ) {
			my $link = $ref->look_down('_tag','a');
			if( $link ){
				my $paper = $link->as_text;
				print "paper : ",$paper,"\n";
				print "link : ",$link->attr('href'),"\n\n";
			}
		}
	}
}else {
	print "no referece.\n";
}

$tree->delete;

#============================  subroutines ===============================

#download the paper according to the url and save it to a file.
#$url : the paper's link.
#$file : the name of the file.
sub download_paper {
	my ($url,$file) = @_;
	getstore($url,$file);
}

